package br.com.hstar.client.util;

import java.util.ArrayList;

import br.com.hstar.client.util.table.SortableTable;

import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.HTMLTable.CellFormatter;
import com.google.gwt.user.client.ui.HTMLTable.RowFormatter;

public class Listagem extends VerticalPanel {

	private SortableTable sortableTable;
	private CellFormatter cellFormatter;
	private RowFormatter rowFormatter;
	private Label title;
			
	public Listagem(String _title) {
		title = new Label(_title);
		title.setStyleName("gwt-LabelMaior");
		this.add(title);
		
		sortableTable = new SortableTable();
		//configs the table
		sortableTableParam(sortableTable);

		add(sortableTable);
	}
	
	//metodos usados para tratar o evendo do click-------------------------------------
	public String getHTMLFromTable(int row, int column){return sortableTable.getHTML(row, column);}
	public SortableTable getSortableTable(){return this.sortableTable;}
	public void setSortColIndex(int col){sortableTable.setSortColIndex(col);}
	public void sort(int col){sortableTable.sort(col);}
	public void drawTable(){sortableTable.drawTable();}
	//---------------------------------------------------------------------------------
	
	public void addColumns(String[] columnNames) {
		//add the column Names
		for(int i=0; i<columnNames.length; i++) {
			sortableTable.addColumnHeader(columnNames[i], i);
		}
	}
	
	public void addContents(String[] columnNames, ArrayList data) {
		//adiciona o nome das colunas
		addColumns(columnNames);

		//add the data content
		for(int i=0; i<data.size(); i++) {
			for(int j=0; j<columnNames.length; j++) {
				HTML html = new HTML( "<A HREF='javascript:;'>"+((KeyValue)data.get(i)).getValue(j)+"</A> ");
				sortableTable.setValue(i+1, j, html.toString());
			}
		}	
		
	    //Set Style Name for the header
		rowFormatter = sortableTable.getRowFormatter();
		rowFormatter.setStyleName(0, "tableHeader");
		
		// Set the Styles for the Data Rows and Columns
		cellFormatter = sortableTable.getCellFormatter();
		// Set the styles for the headers
		for (int colIndex=0; colIndex<columnNames.length; colIndex++){
			cellFormatter.setStyleName(0, colIndex, "headerStyle");
			cellFormatter.setAlignment(0, colIndex, HasHorizontalAlignment.ALIGN_CENTER, HasVerticalAlignment.ALIGN_MIDDLE);
		}

		for(int rowIndex=1; rowIndex<=data.size(); rowIndex++){
			if(rowIndex%2 == 0){
				rowFormatter.setStyleName(rowIndex, "customRowStyle");
			}else{
				rowFormatter.setStyleName(rowIndex, "tableRow");
			}
			for (int colIndex=0; colIndex<columnNames.length; colIndex++){
				cellFormatter.setStyleName(rowIndex, colIndex, "customFont");
				cellFormatter.setAlignment(rowIndex, colIndex, 
						HasHorizontalAlignment.ALIGN_CENTER, 
						HasVerticalAlignment.ALIGN_MIDDLE);
			}
		}
	}
	
	public void sortableTableParam(SortableTable _sortableTable) {
		_sortableTable.setWidth(500 + "px");
	    _sortableTable.setStyleName("sortableTable");
	    _sortableTable.setBorderWidth(1);
	    _sortableTable.setCellPadding(4);
	    _sortableTable.setCellSpacing(1);
	}

}
